package com.zhazha.stand.controller.admin;


import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.zhazha.stand.controller.BaseController;
import com.zhazha.stand.controller.Restful;
import com.zhazha.stand.controller.RestfulController;
import com.zhazha.stand.entity.Department;
import com.zhazha.stand.entity.User;
import com.zhazha.stand.logic.impl.DepartmentLogicImpl;
import com.zhazha.stand.logic.impl.UserLogicImpl;

public class UserController extends RestfulController{
	
	private UserLogicImpl userLogic = new UserLogicImpl();
	private DepartmentLogicImpl dmLogic = new DepartmentLogicImpl();
	private List<User> userList = null;
	
	@Override
	public String create() {
		User user = new User();
		user.setName(request.getParameter("username").trim());
		Department dm = dmLogic.getByName(request.getParameter("department"));
		if (null == dm)
			return ERROR;
		user.setDepartmentId(dm.getId());
		user.setGrd(this.getIntParam("grd"));
		if (!userLogic.insert(user))
			return ERROR;
		return SUCCESS;
	}

	@Override
	public String destroy() {
		// TODO Auto-generated method stub
		if (!userLogic.delete(this.getIntParam("id")))
			return ERROR;
		return SUCCESS;
	}

	//@Override
	public String list() {
		// TODO Auto-generated method stub
		userList = userLogic.getList();
		return SUCCESS;
	}

	@Override
	public String edit() {
		// TODO Auto-generated method stub
		User user = new User();
		user.setId(this.getIntParam("id"));
		user.setName(request.getParameter("name"));
		Department dm = dmLogic.getByName(request.getParameter("department"));
		if (null == dm)
			return ERROR;
		user.setDepartmentId(dm.getId());
		if (!userLogic.update(user))
			return ERROR;
		return SUCCESS;
	}

	@Override
	public String xml() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String json() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String update() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String index() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String editNew() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

}
